package com.redwine.app.utils;

import android.content.Context;
import android.content.res.Resources;
import android.os.IBinder;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;


import com.redwine.app.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by caoyuanjing on 15/12/8.
 */
public class TextViewUtil {
    public Context mContext;
    public Resources mResources;

    public TextViewUtil(Context mContext) {
        this.mContext = mContext;
        mResources = mContext.getResources();
    }

    /**
     * params str  原文
     * params index  关键字
     * 搜索原文是否包含关键字
     */
    public static boolean isIndexStr(String str, String index) {
        str = str.substring(str.length() - 10);
        if (str.indexOf(index) != -1) {
            return true;
        }
        return false;
    }


    /**
     * params str  原文
     * params index  关键字
     * 原文包含关键字的具体位置
     */
    public static int iIndexStr(String str, String index) {
        return str.indexOf(index);
    }


    public String getStringIndex(String str) {
        if (isIndexStr(str, mResources.getString(R.string.click_now_look))) {
            return mResources.getString(R.string.click_now_look);
        }
        return null;
    }

    public String getSearchKey(String str, String message) {
        if (isIndexStr(str, message)) {
            return message;
        }
        return null;
    }

    public String getContent(String str, String index) {
        str = str.substring(0, str.length() - index.length());
        return str;
    }

    public static boolean isReplyEntity(String name, String str) {
        if(TextUtils.isEmpty(name)||TextUtils.isEmpty(str)){
            return false;
        }
        if (str.length() > name.length()) {
            if (isContain(str, name)) {
                str = str.substring(name.length(), str.length()).trim();
                if (str == null || str.length() == 0) {
                    return true;
                } else {
                    return false;
                }
            } else {
                if (str == null || str.length() == 0)
                    return true;
                else {
                    return false;
                }
            }
        } else {
            if (str == null || str.length() == 0)
                return true;
            else {
                return false;
            }
        }
    }

    public static boolean isContain(String str, String name) {
        return str.contains(name);
    }

    public static Spanned SearchKey(String title, String key) {
        if (title.contains(key)) {
            int index = TextViewUtil.iIndexStr(title, key);
            int len = key.length();
            Spanned temp = Html.fromHtml(title.substring(0, index)
                    + "<font color=#F63756 face=verdana size=14>"
                    + title.substring(index, index + len) + "</font>"
                    + title.substring(index + len, title.length()));
            return temp;
        } else {
            return null;
        }
    }

    public static List<Integer> searchKeyPosition(String content, String key) {
        if (content.contains(key)) {
            List<Integer> mlist = new ArrayList<>();
            int cnt = 0;
            int offset = 0;
            while ((offset = content.indexOf(key, offset)) != -1) {
                mlist.add(offset);
                offset = offset + key.length();
                cnt++;
            }
            return mlist;
        }
        return null;
    }

    public static SpannableStringBuilder keyStyleShow(String content, Context mContext) {
        List<Integer> f_keys = searchKeyPosition(content, "<em>");
        List<Integer> e_keys = searchKeyPosition(content, "</em>");
        return keyStyle(content, f_keys, e_keys, mContext);
    }


    public static SpannableStringBuilder keyStyle(String content, List<Integer> f_keys, List<Integer> e_keys, Context mContext) {
        content = content.replaceAll("<em>", "");
        content = content.replaceAll("</em>", "");
        SpannableStringBuilder style = new SpannableStringBuilder(content);
        Log.e("SpannableStringBuilder", "f_keys" + f_keys.size());
        for (int i = 0; i < f_keys.size(); i++) {
            Log.e("SpannableStringBuilder", "SpannableStringBuilder" + i);
            if (i == 0) {
                if (content.length() >= e_keys.get(i) - 4) {
                    style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.search_blue)), f_keys.get(i), e_keys.get(i) - 4, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                }
            } else {
                if (content.length() >= e_keys.get(i) - 13 && (f_keys.get(i) - 9 * i) < e_keys.get(i) - 13 * (i)) {
                    style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.search_blue)), f_keys.get(i) - 9 * i, e_keys.get(i) - 13 * (i), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                }
            }
        }

//        for(int i=0;i<f_keys.size();i++){
//            Drawable d = mContext.getResources().getDrawable(R.drawable.tran);
//            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
//            style.setSpan(span, f_keys.get(i), f_keys.get(i)+4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        }
//        for(int i=0;i<e_keys.size();i++){
//            Drawable d = mContext.getResources().getDrawable(R.drawable.tran);
//            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
//            ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE);
//            style.setSpan(span, e_keys.get(i), e_keys.get(i) + 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//        }
        return style;


    }



    /**
     * 根据EditText所在坐标和用户点击的坐标相对比，来判断是否隐藏键盘，因为当用户点击EditText时则不能隐藏
     *
     * @param v
     * @param event
     * @return
     */
    public static boolean isShouldHideKeyboard(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] l = {0, 0};
            v.getLocationInWindow(l);
            int left = l[0],
                    top = l[1],
                    bottom = top + v.getHeight(),
                    right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击EditText的事件，忽略它。
                return false;
            } else {
                return true;
            }
        }
        // 如果焦点不是EditText则忽略，这个发生在视图刚绘制完，第一个焦点不在EditText上，和用户用轨迹球选择其他的焦点
        return false;
    }


    /**
     * 获取InputMethodManager，隐藏软键盘
     *
     * @param token
     */
    public  static  void hideKeyboard(IBinder token,Context mContext) {
        if (token != null) {
            InputMethodManager im = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
            im.hideSoftInputFromWindow(token, InputMethodManager.HIDE_NOT_ALWAYS);


        }
    }


}
